package com.crawler.waf.controllers;

import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.crawler.waf.exceptions.handlers.ErrorMessageQueueHandler;
import com.crawler.waf.exceptions.messages.ErrorMessage;

@RestController()
@RequestMapping(value="/$waf/errors")
public class ErrorMessageController {
	
	/**
	 * 
	* @Title: getErrorMessage 
	* @Description: 根据requestId查询异常详情 
	* @param @param requestId
	* @param @return    设定文件 
	* @return ErrorMessage    返回类型 
	* @throws
	 */
	@RequestMapping(method = RequestMethod.GET, value="/{requestId}")
	public ErrorMessage getErrorMessage(@PathVariable String requestId){
		ErrorMessage exeInfo = ErrorMessageQueueHandler.getReleasedException(requestId);
		return exeInfo;
	}
	
	/**
	 * 
	* @Title: getErrorMessage 
	* @Description: 查询内存中的所有异常信息
	* @param @return    设定文件 
	* @return BlockingQueue<ErrorMessage>    返回类型 
	* @throws
	 */
	@RequestMapping(method = RequestMethod.GET)
	public ErrorMessage [] getErrorMessage(){
		ErrorMessage [] errorMessages = ErrorMessageQueueHandler.getReleasedExceptionQueue();
		if(errorMessages!=null && errorMessages.length>0){
			for (int i = 0; i < errorMessages.length; i++) {
				ErrorMessage errorMessage = errorMessages[i];
				errorMessage.setMessage(errorMessage.getMessage()+errorMessage.getStackTrace());
			}
		}
		return errorMessages;
	}
}
